package com.boolib.load.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@EnableAsync
@Component
public class MailHelper {


    @Autowired
    private JavaMailSender sender;


    /***
     * 发送HTML邮件
     * @param from  发信人邮箱地址
     * @param to    接收人邮箱地址
     * @param title 邮件标题
     * @param content      邮件内容
     */
    @Async
    public void sendHtmlMail(String from,String to ,String title,String content){


        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        try {


            mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content,true);

            sender.send(mimeMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }



    }



}
